(function () {
    'use strict';

    angular.module('app')
        .controller('AppCtrl', [ '$scope', '$rootScope', '$state', '$document', 'AuthenticationService', AppCtrl]) // overall control
        .config(['$mdThemingProvider', mdConfig])

    function AppCtrl($scope, $rootScope, $state, $document, AuthenticationService) {

        $rootScope.userInformation = AuthenticationService.getUserInformation();

        $scope.main = {
            brand: 'Visual GYM',
            name: 'Lisa'
        };

        $scope.color = {
            primary:    '#3F51B5',
            success:    '#4CAF50',
            info:       '#00BCD4',
            infoAlt:    '#673AB7',
            warning:    '#FFC107',
            danger:     '#F44336',
            gray:       '#DCDCDC'
        };

        $rootScope.$on("$stateChangeSuccess", function (event, currentRoute, previousRoute) {
            $document.scrollTo(0, 0);
        });

        $rootScope.$on('$stateChangeStart', function (event, toState) {
            // if route requires auth and user is not logged in
            if (!(toState.data && toState.data.dontRequireAuth) && !AuthenticationService.isLoggedIn()) {
                // redirect back to login
                $state.go('pages_signin');
                event.preventDefault();
            }
        });

        $rootScope.logout = function() {
            AuthenticationService.logout();
            $state.go('pages_signin');
        };

    }

    function mdConfig($mdThemingProvider) {
        // Available palettes: red, pink, purple, deep-purple, indigo, blue, light-blue, cyan, teal, green, light-green, lime, yellow, amber, orange, deep-orange, brown, grey, blue-grey
        $mdThemingProvider.theme('default')
            .primaryPalette('deep-purple', {
                'default': '500'
            })
            .accentPalette('cyan', {
                'default': '500'
            })
            .warnPalette('red', {
                'default': '500'
            })
            .backgroundPalette('grey');
    }

})();
